local function CreateLabel(text, color, x,y, parent)
	local label = vgui.Create("DLabel", parent) 
	label:SetText(text) 
	label:SetTextColor(color) 
	label:SizeToContents()
	label:SetPos(x,y)
	return label
end

local WHITE = Color(255,255,255,255)
local ORANGE = Color(195,140,45,255)
local DARK_GREY = Color(16,16,16,255)
local GREY = Color(128,128,128,255)

local PADDING = 9
local THIN_PAD = 1
local ANCHOR_Y = PADDING
local ANCHOR_X = PADDING
local CAT_WIDTH = 100
local SEC_WIDTH = 300
local HEIGHT = 250
local TITLE_BAR_HEIGHT = 20
local BUTTON_DIMENSION = 64
local BUTTON_DIMENSION_SMALL = 32
local lastPropertiesSection = 1

local function CreateButton(text, clickFunction, width,height, darken, parent)
	local button = vgui.Create("DButton", parent) 
	button:SetText(text) 
	button:SetTextColor(WHITE) 
	if darken then
		button:SetColor(DARK_GREY)
    end
	button:SizeToContents()
	button:SetTextColorHovered(ORANGE) 
	button:SetSize(width,height)
	button:SetDrawBorder(false)
	button.DoClick = clickFunction
	return button
end

local function LoadCustomParticles(callingEntity, dimen,panel)

	local buttonTable = {}
	
	local defaultButton = vgui.Create( "DImageButton", panel) 
	defaultButton:SetMaterial( "sprites/physg_glow1" ) 
	defaultButton:SetSize(dimen,dimen)
	defaultButton.DoClick = function () 
		
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. callingEntity:EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_UPDATE_PARTICLESYSTEM)
		LocalPlayer():ConCommand( "npc_mover_gun_param_billboard " .. "sprites/physg_glow1")

	end
	table.insert(buttonTable,defaultButton)

	//Load custom library
	/////
	local FACE_DIR = "../materials/myproject/particles/"
	local headFn = file.Find(FACE_DIR .. "*.vmt")
	for _, fn in pairs(headFn) do
		local filename = string.sub(fn,1, string.len(fn) - 4)

		local mateiralFilepath = "myproject/particles/" .. filename

		local button = vgui.Create( "DImageButton", panel) 
		button:SetMaterial( mateiralFilepath ) 
		button:SetSize(dimen,dimen)
		button.DoClick = function () 
			
			LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. callingEntity:EntIndex())
			LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_UPDATE_PARTICLESYSTEM)
			LocalPlayer():ConCommand( "npc_mover_gun_param_billboard " .. mateiralFilepath)

		end
		table.insert(buttonTable,button)
	end

	return buttonTable

end

local function DisplayControlSection(callingEntity, sectionPanel)

	local yPos = THIN_PAD
	local general_parameters = CreateLabel("Particle System:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + general_parameters:GetTall()

	local particlePopulationSlider = vgui.Create( "DNumSlider", sectionPanel ) 
	particlePopulationSlider:SetPos( PADDING,yPos) 
	particlePopulationSlider:SetSize( sectionPanel:GetWide() - (PADDING * 2), 35 )
	particlePopulationSlider:SetText( "Total particles" ) 
	particlePopulationSlider:SetMin( 0 )
	particlePopulationSlider:SetValue( callingEntity:GetNWInt("system_population") )
	particlePopulationSlider:SetMax( 600 )
	particlePopulationSlider:SetDecimals( 0 )
	particlePopulationSlider:SetConVar( "npc_mover_gun_system_population" )
	yPos = yPos + particlePopulationSlider:GetTall() + PADDING
	
	local system_control = CreateLabel("Particle System Control:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + system_control:GetTall() + THIN_PAD

	local psystemPlayButton = vgui.Create("DImageButton", sectionPanel) 
	psystemPlayButton:SetMaterial("gui/psystem_play")
	psystemPlayButton:SetSize(BUTTON_DIMENSION_SMALL,BUTTON_DIMENSION_SMALL)
	psystemPlayButton:SetPos(PADDING,yPos)
	psystemPlayButton.DoClick = function () 
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. callingEntity:EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_UPDATE_PARTICLESYSTEM)
		LocalPlayer():ConCommand( "npc_mover_gun_system_state " .. "1")
	end

	local showOriginCheckbox= vgui.Create( "DCheckBoxLabel", sectionPanel ) //so that on stop click we can set it checked
	local psystemStopButton = vgui.Create("DImageButton", sectionPanel) 
	psystemStopButton:SetMaterial("gui/psystem_stop")
	psystemStopButton:SetSize(BUTTON_DIMENSION_SMALL,BUTTON_DIMENSION_SMALL)
	psystemStopButton:SetPos(PADDING + psystemPlayButton:GetWide() + THIN_PAD,yPos)
	psystemStopButton.DoClick = function () 
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. callingEntity:EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_UPDATE_PARTICLESYSTEM)
		LocalPlayer():ConCommand( "npc_mover_gun_system_state " .. "0")
		showOriginCheckbox:SerValue(1)
	end
	yPos = yPos + psystemStopButton:GetTall() + PADDING
	
	local display = CreateLabel("Display:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + display:GetTall() + THIN_PAD

	
	showOriginCheckbox:SetPos(PADDING, yPos )
	showOriginCheckbox:SetText( "Show origin point" )
	showOriginCheckbox:SizeToContents()
	showOriginCheckbox:SetConVar( "npc_mover_gun_system_show_origin" )
	showOriginCheckbox:SetValue( callingEntity:GetNWInt("system_show_origin") )
	yPos = yPos + showOriginCheckbox:GetTall() + THIN_PAD

end

local function DisplayGenerationSection(callingEntity, sectionPanel)

	local yPos = THIN_PAD
	local particle_displacement = CreateLabel("Random Particle Displacement:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + particle_displacement:GetTall() + THIN_PAD

	local xPos = PADDING
	local xDisplacement = vgui.Create( "DNumSlider", sectionPanel ) 
	xDisplacement:SetPos( xPos,yPos) 
	xDisplacement:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	xDisplacement:SetText( "X" ) 
	xDisplacement:SetMin( 0 )
	xDisplacement:SetMax( 500 )
	xDisplacement:SetValue( callingEntity:GetNWInt("generation_disx") )
	xDisplacement:SetConVar( "npc_mover_gun_generation_disx" )
	xDisplacement:SetDecimals( 1 )
	xPos = xPos + xDisplacement:GetWide() + PADDING

	local yDisplacement = vgui.Create( "DNumSlider", sectionPanel ) 
	yDisplacement:SetPos( xPos,yPos) 
	yDisplacement:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	yDisplacement:SetText( "Y" ) 
	yDisplacement:SetMin( 0 )
	yDisplacement:SetMax( 500 )
	yDisplacement:SetDecimals( 1 )
	yDisplacement:SetValue( callingEntity:GetNWInt("generation_disy") )
	yDisplacement:SetConVar( "npc_mover_gun_generation_disy" )
	xPos = xPos + yDisplacement:GetWide() + PADDING
	
	local zDisplacement = vgui.Create( "DNumSlider", sectionPanel ) 
	zDisplacement:SetPos( xPos,yPos) 
	zDisplacement:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	zDisplacement:SetText( "Z" ) 
	zDisplacement:SetMin( 0 )
	zDisplacement:SetMax( 500 )
	zDisplacement:SetDecimals( 1 )
	zDisplacement:SetValue( callingEntity:GetNWInt("generation_disz") )
	zDisplacement:SetConVar( "npc_mover_gun_generation_disz" )
	xPos = xPos + zDisplacement:GetWide() + PADDING

	xPos = PADDING
///////////////////////////////////////////////
	yPos = yPos + zDisplacement:GetTall() + THIN_PAD
	local particle_velocity = CreateLabel("Particle Velocity:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + particle_velocity:GetTall() + THIN_PAD

	local xDirection = vgui.Create( "DNumSlider", sectionPanel ) 
	xDirection:SetPos( xPos,yPos) 
	xDirection:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	xDirection:SetText( "X" ) 
	xDirection:SetMin( -200 )
	xDirection:SetMax( 200 )
	xDirection:SetValue( callingEntity:GetNWInt("generation_dirx") )
	xDirection:SetConVar( "npc_mover_gun_generation_dirx" )
	xDirection:SetDecimals( 1 )
	xPos = xPos + xDirection:GetWide() + PADDING

	local yDirection = vgui.Create( "DNumSlider", sectionPanel ) 
	yDirection:SetPos( xPos,yPos) 
	yDirection:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	yDirection:SetText( "Y" ) 
	yDirection:SetMin( -200 )
	yDirection:SetMax( 200 )
	yDirection:SetDecimals( 2 )
	yDirection:SetValue( callingEntity:GetNWInt("generation_diry") )
	yDirection:SetConVar( "npc_mover_gun_generation_diry" )
	xPos = xPos + yDirection:GetWide() + PADDING
	
	local zDirection = vgui.Create( "DNumSlider", sectionPanel ) 
	zDirection:SetPos( xPos,yPos) 
	zDirection:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	zDirection:SetText( "Z" ) 
	zDirection:SetMin( -200 )
	zDirection:SetMax( 200 )
	zDirection:SetDecimals( 1 )
	zDirection:SetValue( callingEntity:GetNWInt("generation_dirz") )
	zDirection:SetConVar( "npc_mover_gun_generation_dirz" )
	xPos = xPos + zDirection:GetWide() + PADDING

	local varDirection = vgui.Create( "DNumSlider", sectionPanel ) 
	varDirection:SetPos( xPos,yPos)
	varDirection:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	varDirection:SetText( "Var" ) 
	varDirection:SetMin( 0 )
	varDirection:SetMax( 100 )
	varDirection:SetDecimals( 1 )
	varDirection:SetValue( callingEntity:GetNWInt("generation_dirvar") )
	varDirection:SetConVar( "npc_mover_gun_generation_dirvar" )
	xPos = PADDING

///////////////////////////////////////////////
	yPos = yPos + varDirection:GetTall() + THIN_PAD
	local particle_acceleration = CreateLabel("Particle Acceleration:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + particle_acceleration:GetTall() + THIN_PAD

	local xAcceleration = vgui.Create( "DNumSlider", sectionPanel ) 
	xAcceleration:SetPos( xPos,yPos) 
	xAcceleration:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	xAcceleration:SetText( "X" ) 
	xAcceleration:SetMin( -10 )
	xAcceleration:SetMax( 10 )
	xAcceleration:SetValue( callingEntity:GetNWInt("generation_acex") )
	xAcceleration:SetConVar( "npc_mover_gun_generation_acex" )
	xAcceleration:SetDecimals( 1 )
	xPos = xPos + xAcceleration:GetWide() + PADDING

	local yAcceleration = vgui.Create( "DNumSlider", sectionPanel ) 
	yAcceleration:SetPos( xPos,yPos) 
	yAcceleration:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	yAcceleration:SetText( "Y" ) 
	yAcceleration:SetMin( -10 )
	yAcceleration:SetMax( 10 )
	yAcceleration:SetDecimals( 1 )
	yAcceleration:SetValue( callingEntity:GetNWInt("generation_acey") )
	yAcceleration:SetConVar( "npc_mover_gun_generation_acey" )
	xPos = xPos + yAcceleration:GetWide() + PADDING
	
	local zAcceleration = vgui.Create( "DNumSlider", sectionPanel ) 
	zAcceleration:SetPos( xPos,yPos) 
	zAcceleration:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	zAcceleration:SetText( "Z" ) 
	zAcceleration:SetMin( -10 )
	zAcceleration:SetMax( 10 )
	zAcceleration:SetDecimals( 1 )
	zAcceleration:SetValue( callingEntity:GetNWInt("generation_acez") )
	zAcceleration:SetConVar( "npc_mover_gun_generation_acez" )
	xPos = xPos + zAcceleration:GetWide() + PADDING

	local varAcceleration = vgui.Create( "DNumSlider", sectionPanel ) 
	varAcceleration:SetPos( xPos,yPos)
	varAcceleration:SetSize( ((sectionPanel:GetWide() - PADDING) / 4) - PADDING, 35 )
	varAcceleration:SetText( "Var" ) 
	varAcceleration:SetMin( 0 )
	varAcceleration:SetMax( 1 )
	varAcceleration:SetDecimals( 1 )
	varAcceleration:SetValue( callingEntity:GetNWInt("generation_acevar") )
	varAcceleration:SetConVar( "npc_mover_gun_generation_acevar" )
	xPos = PADDING
///////////////////////////////////////////////////////////
	yPos = yPos + varAcceleration:GetTall() + PADDING

	local randDirButton = vgui.Create("DButton", sectionPanel) 
	randDirButton:SetText("Random")
	randDirButton:SetSize( (sectionPanel:GetWide() / 2) - PADDING, 25 ) 
	randDirButton:SetPos(PADDING,yPos)
	randDirButton.DoClick = function () 
	
	local x = 0
	local y = 0
	local z = 0
	local v = 0

	x = math.random(-10.0,10.0)	xDirection:SetValue(x)
	y = math.random(-10.0,10.0)	yDirection:SetValue(y)
	z = math.random(-10.0,10.0)	zDirection:SetValue(z)
	v = math.random(0,1.0)	varDirection:SetValue(v)
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_dirx %g", x))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_diry %g", y))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_dirz %g", z))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_dirvar %g", v))

	x = math.random(-10.0,10.0)	xDisplacement:SetValue(x)
	y = math.random(-10.0,10.0)	yDisplacement:SetValue(y)
	z = math.random(-10.0,10.0)	zDisplacement:SetValue(z)
	v = math.random(0,1.0)	varDisplacement:SetValue(v)
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_disx %g", x))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_disy %g", y))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_disz %g", z))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_disvar %g", v))

	x = math.random(-10.0,10.0)	xAcceleration:SetValue(x)
	y = math.random(-10.0,10.0)	yAcceleration:SetValue(y)
	z = math.random(-10.0,10.0)	zAcceleration:SetValue(z)
	v = math.random(0,1.0)	varAcceleration:SetValue(v)
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_acex %g", x))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_acey %g", y))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_acez %g", z))
	LocalPlayer():ConCommand( string.format("npc_mover_gun_generation_acevar %g", v))

	LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. callingEntity:EntIndex())
	LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_UPDATE_PARTICLESYSTEM)
	end
end

local function DisplayParametersSection(callingEntity, sectionPanel)

	local yPos = THIN_PAD
	local particle_parameters = CreateLabel("Particle Parameters:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + particle_parameters:GetTall() + THIN_PAD

	local particleRadius = vgui.Create( "DNumSlider", sectionPanel ) 
	particleRadius:SetPos( PADDING,yPos) 
	particleRadius:SetSize( sectionPanel:GetWide() / 2 - (PADDING * 2), 35 )
	particleRadius:SetText( "Radius" ) 
	particleRadius:SetMin( 0 )
	particleRadius:SetMax( 50 )
	particleRadius:SetDecimals( 1 )
	particleRadius:SetValue( callingEntity:GetNWInt("param_rad") )
	particleRadius:SetConVar( "npc_mover_gun_param_rad" )

	local radiusVariation = vgui.Create( "DNumSlider", sectionPanel ) 
	radiusVariation:SetPos( sectionPanel:GetWide() / 2,yPos) 
	radiusVariation:SetSize( (sectionPanel:GetWide() / 2) - (2 * PADDING), 35 )
	radiusVariation:SetText( "Variation" ) 
	radiusVariation:SetMin( 0 )
	radiusVariation:SetMax( 100 )
	radiusVariation:SetDecimals( 0 )
	radiusVariation:SetValue( callingEntity:GetNWInt("param_radvar") )
	radiusVariation:SetConVar( "npc_mover_gun_param_radvar" )
	yPos = yPos + particleRadius:GetTall() + THIN_PAD
	
	local particleLifetime = vgui.Create( "DNumSlider", sectionPanel ) 
	particleLifetime:SetPos( PADDING,yPos) 
	particleLifetime:SetSize( sectionPanel:GetWide() / 2 - (PADDING * 2), 35 )
	particleLifetime:SetText( "Lifetime" ) 
	particleLifetime:SetMin( 0 )
	particleLifetime:SetMax( 100 )
	particleLifetime:SetDecimals( 1 )
	particleLifetime:SetValue( callingEntity:GetNWInt("param_life") )
	particleLifetime:SetConVar( "npc_mover_gun_param_life" )

	local lifetimeVariation = vgui.Create( "DNumSlider", sectionPanel ) 
	lifetimeVariation:SetPos( sectionPanel:GetWide() / 2,yPos) 
	lifetimeVariation:SetSize( (sectionPanel:GetWide() / 2) - (2 * PADDING), 35 )
	lifetimeVariation:SetText( "Variation" ) 
	lifetimeVariation:SetMin( 0 )
	lifetimeVariation:SetMax( 100 )
	lifetimeVariation:SetDecimals( 0 )
	lifetimeVariation:SetValue( callingEntity:GetNWInt("param_lifevar") )
	lifetimeVariation:SetConVar( "npc_mover_gun_param_lifevar" )
	yPos = yPos + lifetimeVariation:GetTall() + THIN_PAD
	
	local particleRotation = vgui.Create( "DNumSlider", sectionPanel ) 
	particleRotation:SetPos( PADDING,yPos) 
	particleRotation:SetSize( sectionPanel:GetWide() / 2 - (PADDING * 2), 35 )
	particleRotation:SetText( "Rotation speed" ) 
	particleRotation:SetMin( -10 )
	particleRotation:SetMax( 10 )
	particleRotation:SetDecimals( 1 )
	particleRotation:SetValue( callingEntity:GetNWInt("param_rotate") )
	particleRotation:SetConVar( "npc_mover_gun_param_rotate" )

	local rotationVariation = vgui.Create( "DNumSlider", sectionPanel ) 
	rotationVariation:SetPos( sectionPanel:GetWide() / 2,yPos) 
	rotationVariation:SetSize( (sectionPanel:GetWide() / 2) - (2 * PADDING), 35 )
	rotationVariation:SetText( "Variation" ) 
	rotationVariation:SetMin( 0 )
	rotationVariation:SetMax( 100 )
	rotationVariation:SetDecimals( 0 )
	rotationVariation:SetValue( callingEntity:GetNWInt("param_rotatevar") )
	rotationVariation:SetConVar( "npc_mover_gun_param_rotatevar" )
	yPos = yPos + rotationVariation:GetTall() + PADDING

	local custom_particles = CreateLabel("Particle Library:", GREY, PADDING,yPos,sectionPanel )
	yPos = yPos + custom_particles:GetTall() + THIN_PAD
	
	local customParticlesButtons = LoadCustomParticles(callingEntity,BUTTON_DIMENSION_SMALL,sectionPanel)
	local count = 0
	for _, particleButton in pairs(customParticlesButtons) do

		local inter, frac = math.modf(sectionPanel:GetWide() / (PADDING + BUTTON_DIMENSION_SMALL))
		local col = count % inter
		local line, ig = math.modf(count / inter)
		particleButton:SetPos(PADDING + (col * (THIN_PAD + BUTTON_DIMENSION_SMALL)), (line * (THIN_PAD + BUTTON_DIMENSION_SMALL)) + yPos)
	
		count = count + 1
		
	end
end

local function ShowParticleSystemProperties(userMessage)

	local callingEntity = userMessage:ReadEntity()
	local screenPos = callingEntity:GetPos():ToScreen()
	local secPanel = nil

	local camerapathwayPropertiesDialog = vgui.Create( "DFrame" ) 
	camerapathwayPropertiesDialog:SetPos( screenPos.x - 200,screenPos.y + 30) 
	camerapathwayPropertiesDialog:SetSize( PADDING + CAT_WIDTH + PADDING + SEC_WIDTH + PADDING, HEIGHT ) 
	camerapathwayPropertiesDialog:SetTitle( "Properties")
	camerapathwayPropertiesDialog:SetVisible( true ) 
	camerapathwayPropertiesDialog:SetDraggable( false )
	camerapathwayPropertiesDialog:ShowCloseButton( true )
	camerapathwayPropertiesDialog:MakePopup() 
    
	local catList = vgui.Create( "DPanelList", camerapathwayPropertiesDialog ) 
	catList:SetPos( PADDING,TITLE_BAR_HEIGHT + PADDING ) 
	catList:SetSize( CAT_WIDTH, HEIGHT - (PADDING * 2) - TITLE_BAR_HEIGHT)
	catList:SetSpacing(1)
	catList:SetPadding(1)
	catList:EnableHorizontal(false)
	catList:EnableVerticalScrollbar(true)		 

	local rebuildSectionPanel = function()
		
		if secPanel then
			secPanel:SetVisible(false)
		end

		secPanel = vgui.Create( "DPanel", camerapathwayPropertiesDialog) 
		secPanel:SetPos( PADDING + CAT_WIDTH + PADDING,TITLE_BAR_HEIGHT + PADDING ) 
		secPanel:SetSize( SEC_WIDTH, HEIGHT - (PADDING * 2) - TITLE_BAR_HEIGHT )
		secPanel.Paint = function()
			surface.SetDrawColor(50,50,50,255)
			surface.DrawRect(0,0,secPanel:GetWide(),secPanel:GetTall())
		end
	end

	local controlFunc = function () 
		rebuildSectionPanel()
		lastPropertiesSection = 1
		DisplayControlSection(callingEntity,secPanel)
	end

	local generationFunc = function () 
		rebuildSectionPanel()
		lastPropertiesSection = 2
		DisplayGenerationSection(callingEntity,secPanel)
	end

	local parametersFunc = function () 
		rebuildSectionPanel()
		lastPropertiesSection = 3
		DisplayParametersSection(callingEntity,secPanel)
	end

	local closeFunc = function ()
		//Doing this ensures that data from slider controls is updated on server
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. callingEntity:EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_UPDATE_PARTICLESYSTEM)
		
		camerapathwayPropertiesDialog:SetVisible(false)
	end

	local applyFunc = function ()
		//Doing this ensures that data from slider controls is updated on server
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. callingEntity:EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_UPDATE_PARTICLESYSTEM)
		
	end

	catList:AddItem(CreateLabel("System",GREY, PADDING, ANCHOR_Y, catList))
	catList:AddItem(CreateButton("Control", controlFunc, CAT_WIDTH,15, false, catList))

	catList:AddItem(CreateLabel("Particles",GREY, PADDING, ANCHOR_Y, catList))
	
	catList:AddItem(CreateButton("Generation", generationFunc, CAT_WIDTH,15, false, catList))
	catList:AddItem(CreateButton("Parameters", parametersFunc, CAT_WIDTH,15, false, catList))

	catList:AddItem(CreateLabel("General",GREY, PADDING, ANCHOR_Y, catList))
	catList:AddItem(CreateButton("Apply", applyFunc, CAT_WIDTH,15, false, catList))
	catList:AddItem(CreateButton("Close", closeFunc, CAT_WIDTH,15, false, catList))

	//Show last playback section
	if lastPropertiesSection == 1 then
		controlFunc()
	elseif lastPropertiesSection == 2 then
		generationFunc()
	elseif lastPropertiesSection == 3 then
		parametersFunc()
	elseif lastPropertiesSection == 4 then
		appearannceFunc()
	end
end

usermessage.Hook( "ShowProperties" .. "sent_particlesystem", ShowParticleSystemProperties) 